import { Component, OnInit,Input,EventEmitter,Output } from '@angular/core';
import {Service} from '../../server/server';
import {HttpClient} from '@angular/common/http';
import {CudTableService} from '../../public/serve/cud-table/cud-table.service';
import {Pagination} from '../../public/serve/pcrudInfo';
import {RemoveTableService} from '../../public/serve/remove-table/remove-table.service';
import {HintsService} from '../../public/serve/hints/hints.service';
import { ActivatedRoute, Params } from '@angular/router';
declare var $:any;

@Component({
    selector: 'app-internal-indexed-modal-table-version',
    templateUrl: './internal-indexed-modal-table-version.component.html',
    styleUrls: ['./internal-indexed-modal-table-version.component.css']
})
export class InternalIndexedModalTableVersionComponent implements OnInit {
    public internalParam : any;
    @Input() internalVersionModalTable:any = '';
    isSelectAll : boolean = false;
    isChose : boolean = false;
    public tabVal: any;
    public detailTabVal: any;
    @Input() paramData: any;
    @Input() searchDataMore: any;
    public totalItems: number;
    public detailTotalItems: number;
    public delete :any;
    currentVersionId = '';
    public currentVersionName = '';
    public currentShowVersionName = '';
    currentVersionActive = -1;
    taskType:any;

    constructor(private activatedRoute: ActivatedRoute,private hintsService: HintsService,public removeTableService: RemoveTableService, private server: Service,private http: HttpClient, public request: CudTableService,) { }

    ngOnInit() {
        // this.getIndexedVersionTables(
        //     this.pageNumber,
        //     this.pageSize,
        //     this.paramData,
        //     this.searchDataMore
        // );
        this.activatedRoute.queryParams.subscribe((p: Params) => {
            this.taskType = p['taskType'];
        });
    }


    /**
     * 查询标引版本列表
     */
    getIndexedVersionTables(pageNumber, pageSize,paramData, queryCriteria){
        let url = this.server.baseUrl + 'Service/IdxProduct/getProductIndexingVersion';
        let data = {
            "token": "b25dbd7138cf48e09941087e6d9a2e93",
            // "checkStatus":1,
            "productId":this.internalParam.productId,
            "taskId":this.internalParam.taskId,
            "id":this.internalParam.id,
            "pageNo":pageNumber,
            "pageSize":pageSize,
            "queryCriteria":queryCriteria
        };
        this.request.postRequest(url,data).then((res:any) => {
            if (res.result_code === '0000') {
                this.tabVal = res.result;
                this.totalItems = res.length;
            }else{
                // this.hintsService._operationHints(res.message,res.result_code);
            }
            // this._cancelChecked();
            return;
        })
            .catch((err: any)=>{
                // this.hintsService._operationHints("error","error");
                // this._cancelChecked();
                return;
            });
    }


    /**
     * 查询标引版本列表
     */
    getIndexedVersionDetailTables(pageNumber, pageSize,versionId){
        let url = this.server.baseUrl + 'Service/innertext/getVersionThemeWordsList';
        let data = {
            "token": "b25dbd7138cf48e09941087e6d9a2e93",
            "versionId":versionId,
            "themeWordTableIds":[],
            "productId":this.internalParam.productId,
            "taskId":this.internalParam.taskId,
            "pageNo":pageNumber,
            "pageSize":pageSize
        };
        this.request.postRequest(url,data).then((res:any) => {
            if (res.result_code === '0000') {
                if(res.result.length > 0){
                    if(this.taskType != 2){
                        this.detailTabVal = res.result[0]['productIndexingVersionHis'];
                        this.detailTotalItems = res.result[0]['productIndexingVersionHis'].length;
                    }else{
                        this.detailTabVal = res.result[0]['productThemeRelationHisList'];
                        this.detailTotalItems = res.result[0]['productThemeRelationHisList'].length;
                    }

                }else{
                    this.detailTabVal = {};
                    this.detailTotalItems = 0;
                }

            }else{
                this.hintsService.messages(res.message,res.result_code);
            }
            // this._cancelChecked();
            return;
        })
            .catch((err: any)=>{
                this.hintsService.messages('error','error');
                // this._cancelChecked();
                return;
            });
    }

    // 表格
    tableNumber: number = 0;
    // 当前页（默认为第一页）
    public pageNumber: number = 1;

    // 每页显示多少条数据（默认每页显示20条）
    public pageSize: number = 20;
    // id
    public id: string = '0';
    // 分页
    public pagination: Pagination;
    isLoadTableFromPagination(pagination: Pagination) {
        this.pagination = pagination;

        this.pageNumber = this.pagination[0].pageNumber;
        this.pageSize = this.pagination[0].pageSize;
        this.id = this.pagination[0].id;

        this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

        this.getIndexedVersionDetailTables(
            this.pageNumber,
            this.pageSize,
            this.currentVersionId
        );
    }

    /**
     * 全选
     * @param bool
     * @param keywordTabVal
     */
    _selectAll(bool,keywordTabVal){
        this.isSelectAll = this.isChose = this.removeTableService.keywordRemoveAll(bool, keywordTabVal, 'id');
        if (this.isSelectAll == false) {
            this.getIndexedVersionTables(
                this.pageNumber,
                this.pageSize,
                this.paramData,
                this.searchDataMore
            );
        }
    }

    /**
     * 单选
     * @param bool
     * @param themeWordsId
     * @param length
     */
    _chose(bool, id, length){
        this.isSelectAll = this.removeTableService.keywordRemoveChose(bool, id, length)['bool'];
    }

    /**
     * 重置表格复选框、选中的ID
     */
    private _cancelChecked(){
        this.isChose = this.isSelectAll = false;
        this.removeTableService._clearId("id");
    }

    // //查询
    // search() {
    //     this.getIndexedVersionTables(
    //         this.pageNumber,
    //         this.pageSize,
    //         this.paramData,
    //         this.searchDataMore
    //     );
    // }
    // //响应搜索
    // changesearch() {
    //     this.getIndexedVersionTables(
    //         this.pageNumber,
    //         this.pageSize,
    //         this.paramData,
    //         this.searchDataMore
    //     );
    // }

    //点击查询详情
    _detail(id,name){
        this.currentVersionActive = id;
        this.currentVersionId = id;
        this.currentShowVersionName = name;
        this.getIndexedVersionDetailTables(
            this.pageNumber,
            this.pageSize,
            id
        );
    }

    _useVersionTips(id,name){
        this.currentVersionId = id;
        this.currentVersionName = name;
        $('#useVersionTips').modal('show');
    }

    _useVersion(){
        let info = {};
        info['taskId'] = this.internalParam.taskId;
        info['productId'] = this.internalParam.productId;
        info['versionId'] = this.currentVersionId;

        let url = this.server.baseUrl + 'Service/IdxProduct/setProductIndexingVersionDefault';
        this.request
            .postRequest(url,info)
            .then(res =>{
                if(res.result_code == "0000"){
                    this.server.internalindexeddeleteentEmit.emit(true);
                }
                this.hintsService.messages(res.message,res.result_code);
                return;
            })
            .catch(err =>{
                this.hintsService.messages("error","error");
                return;
            });
    }
}
